package com.indobrains.uud1945;

import android.app.Dialog;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.indobrains.uud1945.database.DatabaseAdapter;
 

public class SearchActivity extends ListActivity implements OnClickListener {
	private static final String SEARCH_KEYWORD = "_keyword";
	private static final String PAGE_NO="PAGE_NO";
	private static final String PAGE_TITLE="PAGE_TITLE";
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		ListView lv=getListView(); 
		Bundle b=getIntent().getExtras();
		String _keyword=b.getString(SEARCH_KEYWORD);
		setSearchAdapter(lv,_keyword);
	}
	private static final String TAG = "Search Activity";
	private DatabaseAdapter db;
	private Cursor cursor;
	private Intent intent;
	private Bundle bundle;
	private Dialog SearchDialog;
	private EditText keyword;
	private Button btnSearch;
	 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search_layout);
		setTitle("Hasil Pencarian Pasal");
		db=new DatabaseAdapter(this);
		db.Open();
		ListView lv=getListView(); 
		Bundle b=getIntent().getExtras();
		String _keyword=b.getString(SEARCH_KEYWORD);
		setSearchAdapter(lv,_keyword);
		
	}
	public void setSearchAdapter(ListView lview,String _keyword){
		try {
			
			cursor=db.searchContents(_keyword);
			
			String[] from = new String[] { db.CLAUSE_TITLE,
					"chapter_name","chapter_description",db.CONTENT_CONTENT,db.AMANDEMEN_DESC,db.CONTENT_OLDER };
			int[] to = new int[] { R.id.search_title,R.id.search_chapter,R.id.search_chapter,
					R.id.search_content,R.id.search_amandemen,R.id.search_older};
			//MenuAdapter adapter=new MenuAdapter(getActivity(), R.layout.list_item, cursor, from, to);
			SearchAdapter adapter = new SearchAdapter(this,R.layout.search_item,cursor,from,to);
			//DasarHukumAdapter adapter=new DasarHukumAdapter(this,R.layout.dasar_hukum,cursor,from,to);
			if (cursor.getCount()>0){
			lview.setAdapter(adapter);
			}
			//setListAdapter(adapter);
		} catch (Exception ex){
			Log.e(TAG,ex.getMessage());
		}
	}
	public void searchDialog(){
		SearchDialog=new Dialog(this);
		SearchDialog.setTitle("Masukkan kata kunci");
		SearchDialog.setContentView(R.layout.search_dialog);
		keyword=(EditText)SearchDialog.findViewById(R.id.keyword);
		btnSearch=(Button)SearchDialog.findViewById(R.id.btnSearch);
		
		btnSearch.setOnClickListener(search_button_click_listener);
		SearchDialog.show();
	}
	private OnClickListener search_button_click_listener = new OnClickListener() {
	    public void onClick(View v) {
	    	String _keyword=keyword.getText().toString();
			setSearchAdapter(getListView(),_keyword);
			Bundle b=new Bundle();
	    	b.putString(SEARCH_KEYWORD, _keyword);
	    	intent.putExtras(b);
	    	SearchDialog.dismiss();
	    	 
	    	
	    }
	};
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		getMenuInflater().inflate(R.menu.chapter_menu, menu);
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch(item.getItemId()){
		case R.id.preface:
			intent=new Intent(this,ContentActivity.class);
	    	bundle = new Bundle();
	    	bundle.putInt(PAGE_NO, 0); //Your id
	    	bundle.putString(PAGE_TITLE, "Kata Pengantar");
			intent.putExtras(bundle);
	    	startActivity(intent);
	    	break;
		case R.id.preambule:
			intent=new Intent(this,ContentActivity.class);
			bundle = new Bundle();
			bundle.putInt(PAGE_NO, 1); //Your id
			bundle.putString(PAGE_TITLE, "Pembukaan");
			intent.putExtras(bundle);
	    	startActivity(intent); 
			break;
		case R.id.search:
			 searchDialog();
			break;
		case R.id.about:
			intent=new Intent(this,ContentActivity.class);
			bundle = new Bundle();
			bundle.putInt(PAGE_NO, 2); //Your id
			bundle.putString(PAGE_TITLE, "Tentang Program");
			intent.putExtras(bundle);
	    	startActivity(intent); 
			break;
		 
		}
		return false;
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v.getId()==R.id.btnSearch){
			
		}
	}

}
